<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Admin\Controller;

use Think\Controller;
class ExcelController extends Controller {

    //put your code here
    //导出订单数据
    public function expOrderExcel() {

        $gid = I('order_state');
        $ex = "2003";
        if ($gid > 0) {
            $links_list = D('Order')->relation('OrderGoods')->where('order_state=%d', $gid)->select();
        } else {
            $links_list = D('Order')->relation('OrderGoods')->select();
        }
        
        foreach ($links_list as $k => $v) {
            $links_list[$k]['tdd_fee'] = Snapup($v['pay_sn']);
            $links_list[$k]['tdd_fee_name'] = SnapupList($v['pay_sn']);
            if($links_list[$k]['refund_state'] == 1){
                $links_list[$k]['order_state'] ='申请退款';
            }else{
                switch ($links_list[$k]['order_state']) {
                    case 0:
                        $links_list[$k]['order_state'] ='订单取消';
                        break;
                    case 10:
                        $links_list[$k]['order_state'] ='待付款';
                        break;
                    case 20:
                        $links_list[$k]['order_state'] ='已付款';
                        break;
                    case 30:
                        $links_list[$k]['order_state'] ='已发货';
                        break;
                    case 40:
                        $links_list[$k]['order_state'] ='订单完成';
                        break;
                    default:
                        break;
                }
            }
            
            $where['order_id'] = $v['order_id'];
            $reciver_info = M('order_common')->where($where)->getField('reciver_info');
            $links_list[$k]['reciver_info'] = unserialize($reciver_info);
            
        }
        
	Vendor('Classes.PHPExcel');
	$objExcel = new \PHPExcel();
        //设置属性 (这段代码无关紧要，其中的内容可以替换为你需要的)  
        $objExcel->getProperties()->setCreator("taodudu");
        $objExcel->getProperties()->setLastModifiedBy("taodudu");
        $objExcel->getProperties()->setTitle("Office 2003 XLS Test Document");
        $objExcel->getProperties()->setSubject("Office 2003 XLS Test Document");
        $objExcel->getProperties()->setDescription("Test document for Office 2003 XLS, generated using PHP classes.");
        $objExcel->getProperties()->setKeywords("office 2003 openxml php");
        $objExcel->getProperties()->setCategory("Test result file");
        $objExcel->setActiveSheetIndex(0);

        $i = 0;
        //表头  
        $k1 = "订单号";
        $k2 = "支付单号";
        $k3 = "店铺";
        $k4 = "买家";
        $k5 = "商品名称";
        $k6 = "商品单价";
        $k7 = "商品数量";
        $k8 = "商品属性";
        $k9 = "商品支付价格";
        $k10 = "支付时间";
        $k11 = "店铺优惠";
        $k12 = "平台优惠";
        $k13 = "运费价格";
        $k14 = "订单状态";
        $k15 = '收货人';
        $k16 = '收货人电话';
        $k17 = '详细地址';

        $objExcel->getActiveSheet()->setCellValue('a1', "$k1");
        $objExcel->getActiveSheet()->setCellValue('b1', "$k2");
        $objExcel->getActiveSheet()->setCellValue('c1', "$k3");
        $objExcel->getActiveSheet()->setCellValue('d1', "$k4");
        $objExcel->getActiveSheet()->setCellValue('e1', "$k5");
        $objExcel->getActiveSheet()->setCellValue('f1', "$k6");
        $objExcel->getActiveSheet()->setCellValue('g1', "$k7");
        $objExcel->getActiveSheet()->setCellValue('h1', "$k8");
        $objExcel->getActiveSheet()->setCellValue('i1', "$k9");
        $objExcel->getActiveSheet()->setCellValue('j1', "$k10");
        $objExcel->getActiveSheet()->setCellValue('k1', "$k11");
        $objExcel->getActiveSheet()->setCellValue('l1', "$k12");
        $objExcel->getActiveSheet()->setCellValue('m1', "$k13");
        $objExcel->getActiveSheet()->setCellValue('n1', "$k14");
        $objExcel->getActiveSheet()->setCellValue('o1', "$k15");
        $objExcel->getActiveSheet()->setCellValue('p1', "$k16");
        $objExcel->getActiveSheet()->setCellValue('q1', "$k17");
        //debug($links_list);  
        foreach ($links_list as $ke => $val) {
            foreach ($links_list[$ke]['OrderGoods'] as $k => $v) {
                $u1 = $i + 2;
                /* ----------写入内容------------- */
                $objExcel->getActiveSheet()->setCellValue('a' . $u1, ' '.$val["order_sn"]);
                $objExcel->getActiveSheet()->setCellValue('b' . $u1, ' '.$val["pay_sn"]);
                $objExcel->getActiveSheet()->setCellValue('c' . $u1, $val["store_name"]);
                $objExcel->getActiveSheet()->setCellValue('d' . $u1, $val["user_name"]);
                $objExcel->getActiveSheet()->setCellValue('e' . $u1, $v["goods_title"]);
                $objExcel->getActiveSheet()->setCellValue('f' . $u1, $v["goods_price"]);
                $objExcel->getActiveSheet()->setCellValue('g' . $u1, $v["goods_num"]);
                $objExcel->getActiveSheet()->setCellValue('h' . $u1, $v["goods_attri_info"]);
                $objExcel->getActiveSheet()->setCellValue('i' . $u1, $v["goods_pay_price"]);
                $objExcel->getActiveSheet()->setCellValue('j' . $u1, date("Y-m-d H:i:s", $val["payment_time"]));
                $objExcel->getActiveSheet()->setCellValue('k' . $u1, $val["discount_fee"]);
                $objExcel->getActiveSheet()->setCellValue('l' . $u1, '优惠：'.$val["tdd_fee"].'元；'.$val["tdd_fee_name"]);
                $objExcel->getActiveSheet()->setCellValue('m' . $u1, $val["shipping_fee"]);
                $objExcel->getActiveSheet()->setCellValue('n' . $u1, $val["order_state"]);
                
                $objExcel->getActiveSheet()->setCellValue('o' . $u1, $val["reciver_info"]['true_name']);
                $objExcel->getActiveSheet()->setCellValue('p' . $u1, ' '.$val["reciver_info"]['mob_phone']);
                $objExcel->getActiveSheet()->setCellValue('q' . $u1, $val["reciver_info"]['area_info'].$val["reciver_info"]['address']);
                $i++;
            }
        }
        // 高置列的宽度  
        $objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
        $objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
        $objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
        $objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(60);
        $objExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
        $objExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
        $objExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
        $objExcel->getActiveSheet()->getColumnDimension('I')->setWidth(10);
        $objExcel->getActiveSheet()->getColumnDimension('J')->setWidth(20);
        $objExcel->getActiveSheet()->getColumnDimension('K')->setWidth(10);
        $objExcel->getActiveSheet()->getColumnDimension('L')->setWidth(50);
        $objExcel->getActiveSheet()->getColumnDimension('M')->setWidth(10);
        $objExcel->getActiveSheet()->getColumnDimension('N')->setWidth(10);
        $objExcel->getActiveSheet()->getColumnDimension('O')->setWidth(10);
        $objExcel->getActiveSheet()->getColumnDimension('P')->setWidth(10);
        $objExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(50);
        $objExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D');
        $objExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objExcel->getProperties()->getTitle() . '&RPage &P of &N');

        // 设置页方向和规模  
        $objExcel->getActiveSheet()->getPageSetup()->setOrientation('portrait');
        $objExcel->getActiveSheet()->getPageSetup()->setPaperSize('9');
        $objExcel->setActiveSheetIndex(0);
        $timestamp = time();
        if ($ex == '2007') { //导出excel2007文档  
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="订单列表 ' . $timestamp . '.xlsx"');
            header('Cache-Control: max-age=0');
            $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
            $objWriter->save('php://output');
            exit;
        } else {  //导出excel2003文档  
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="订单列表' . $timestamp . '.xls"');
            header('Cache-Control: max-age=0');
            $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
            $objWriter->save('php://output');
            exit;
        }
    }
    

}
